package com.hui.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hui.entity.dao.StoreProduct;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 商品表(StoreProduct)表数据库访问层
 *
 * @author hui
 * @since 2022-07-12 23:08:11
 */
public interface StoreProductMapper extends BaseMapper<StoreProduct> {
    /**
     * 增加一次商品的浏览量
     *
     * @param productId 商品的ID
     * @return
     */
    @Update({"update store_product set browse=browse+1 where id=#{productId}"})
    int addBrowseNum(@Param("productId") Long productId);

    @Update({"update store_product set stock=stock-#{num}, sales=sales+#{num} where id=#{productId} and stock >= #{num}"})
    int decStockIncSales(@Param("num") Integer num, @Param("productId") Long productId);

    @Update({"update store_product set stock=stock+#{num}, sales=sales-#{num} where id=#{productId}"})
    int incStockDecSales(@Param("num") Integer num, @Param("productId") Long productId);

    @Update({"update store_product set is_show = #{status} where id = #{id}"})
    void updateOnsale(@Param("status") Integer status, @Param("id") Long id);

    @Update({"update store_combination set stock=stock-#{num}, sales=sales+#{num} where id=#{activityId} and stock >= #{num}"})
    int decCombinationStockIncSales(@Param("num") Integer num, @Param("productId") Long productId, @Param("activityId") Long activityId);

    @Update({"update store_seckill set stock=stock-#{num}, sales=sales+#{num} where id=#{activityId} and stock >= #{num}"})
    int decSeckillStockIncSales(@Param("num") Integer num, @Param("productId") Long productId, @Param("activityId") Long activityId);

    @Update({"update store_combination set stock=stock+#{num}, sales=sales-#{num} where id=#{activityId} and stock >= #{num}"})
    void incCombinationStockIncSales(@Param("num") Integer num, @Param("productId") Long productId, @Param("activityId") Long activityId);

    @Update({"update store_seckill set stock=stock+#{num}, sales=sales-#{num} where id=#{activityId} and stock >= #{num}"})
    void incSeckillStockIncSales(@Param("num") Integer num, @Param("productId") Long productId, @Param("activityId") Long activityId);

    @Update({"update store_product set browse=browse+1 where id=#{productId}"})
    int incBrowseNum(@Param("productId") Long productId);

    @Delete({"DELETE from system_attachment where name like CONCAT(#{id},'_%',#{name}, '%')"})
    void deleteForwardImg(@Param("id") Long id, @Param("name") String name);

    @Select({"SELECT id,spec_type,ot_price,mer_use,description,is_postage,is_sub,is_best,(sales+ficti) as sales,price,is_bargain,vip_price,store_name,stock,keyword,image,cost,is_good,unit_name,is_benefit,update_time,give_integral,is_new,sort,slider_image,is_show,bar_code,postage,code_path,create_time,cate_id,is_seckill,mer_id,temp_id,ficti,store_info,is_del,is_hot,is_integral,integral,browse FROM store_product ${ew.customSqlSegment}"})
    <E extends IPage<StoreProduct>> E selectPage(E page, @Param("ew") Wrapper<StoreProduct> queryWrapper);
}

